package com.whz.workmanageol.controller;

import com.whz.workmanageol.entity.User;
import com.whz.workmanageol.service.UpdateUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class UpdateUserInfoController {

    @Autowired
    private UpdateUserInfoService updateUserInfoService;

    @RequestMapping(value = "/goToPersonInfo")
    public String goToPersonInfo(){
        return "bootstrap/personalinfo";
    }

    @ResponseBody
    @PostMapping(value = "updateEmail")
    public Boolean updateEmail(String newemail, HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        if(updateUserInfoService.updataEmail(user, newemail)){
            user.setEmail(newemail);
            request.getSession().setAttribute("user", user);
            return true;
        }
        return false;
    }

    @ResponseBody
    @PostMapping(value = "updateTel")
    public Boolean updateTel(String newtel, HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        // 用户重新登录
        if(null == user){
            try {
                response.sendRedirect("bootstrap/login");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(updateUserInfoService.updataTel(user, newtel)){
            user.setTel(newtel);
            request.getSession().setAttribute("user", user);
            return true;
        }
        return false;
    }

    @PostMapping(value = "updatePassword")
    public String updatePassword(String oldpsw, String newpsw, HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(!user.getPassword().equals(oldpsw) || "".equals(newpsw) || null == newpsw) {
            if(null != out)
            out.write("<script>alert('密码更新失败，检查原密码是否正确并且新密码不能为空！');</script>");
            return "/bootstrap/index";
        }

        if(updateUserInfoService.updataPassword(user, newpsw)){
            user.setPassword(newpsw);
            request.getSession().setAttribute("user", user);
            if(null != out)
            out.write("<script>alert('密码更新成功，请重新登录');</script>");
            return "/bootstrap/login";
        }
        if(null != out)
        out.write("<script>alert('密码更新失败!请重新填写');</script>");
        return "/bootstrap/index";
    }

    @RequestMapping(value = "logout")
    public String logout(HttpServletRequest request, HttpServletResponse response){
        request.getSession().removeAttribute("user");
        return "bootstrap/login";
    }
}
